package com.bwie.controller;

import com.bwie.domain.TbProd;
import com.bwie.mapper.TbProdMapper;
import com.bwie.poi.ExcelUtil;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/10/18 9:06
 */

@RestController
@RequestMapping(value = "/prod/excel")
public class ExcelController {

    @Autowired
    TbProdMapper tbProdMapper;

    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<TbProd> list = tbProdMapper.selectList(null);
        ExcelUtil<TbProd> util = new ExcelUtil<TbProd>(TbProd.class);
        util.exportExcel(response, list, "产品数据");
    }


    @PostMapping(value = "/import")
    public R importExcel(@RequestPart("file") MultipartFile file) throws IOException {
        ExcelUtil<TbProd> util = new ExcelUtil<TbProd>(TbProd.class);
        List<TbProd> tbProds = util.importExcel(file.getInputStream());
        for (TbProd tbProd : tbProds) {
            System.out.println(tbProd.getProdName());
            tbProdMapper.insert(tbProd);
        }

        return R.OK();
    }


}
